from machine import SPI,Pin
import st7789
import utime
import ujson
import gc
import network
import urequests
# from turtle_nano import Turtle
import gesture_sensor as gs
import mymap

spi=SPI(0,baudrate=40000000,polarity=1,phase=0,bits=8,endia=0,sck=Pin(6),mosi=Pin(8))
display=st7789.ST7789(spi,240,240,reset=Pin(11,func=Pin.GPIO,dir=Pin.OUT),dc=Pin(7,func=Pin.GPIO,dir=Pin.OUT))
display.init()
display.fill(st7789.color565(50,100,200))
# pic = Turtle(display , 87 , 107, st7789.color565(0, 0, 0))
# pic.pensize(2)
# mycolor=[color565(255,255,255),color565(255,0,0),color565(0,255,0),color565(0,0,255),color565(255,255,0)]
# utime.sleep_ms(1000)
# for i in range(5):
#     pic.pencolor(mycolor[i])
#     pic.forward(60)
#     pic.right(144)
#     utime.sleep_ms(300)
display.draw_string(65,80,'Ce',color=st7789.color565(255,255,255),\
                    bg=st7789.color565(50,100,200),size=3)
utime.sleep_ms(150)
display.draw_string(100,80,'nt',color=st7789.color565(255,255,255),\
                    bg=st7789.color565(50,100,200),size=3)
utime.sleep_ms(150)
display.draw_string(135,80,'ro',color=st7789.color565(255,255,255),\
                    bg=st7789.color565(50,100,200),size=3)
utime.sleep_ms(150)
display.draw_string(45,130,'Sy',color=st7789.color565(255,255,255),\
                    bg=st7789.color565(50,100,200),size=3)
utime.sleep_ms(150)
display.draw_string(80,130,'mm',color=st7789.color565(255,255,255),\
                    bg=st7789.color565(50,100,200),size=3)
utime.sleep_ms(150)
display.draw_string(115,130,'et',color=st7789.color565(255,255,255),\
                    bg=st7789.color565(50,100,200),size=3)
utime.sleep_ms(150)
display.draw_string(150,130,'ry',color=st7789.color565(255,255,255),\
                    bg=st7789.color565(50,100,200),size=3)
utime.sleep_ms(500)

while 1:
    display.draw_string(38,190,'up to start...',color=st7789.color565(200,50,200),\
                        bg=st7789.color565(50,100,200),size=2)
    utime.sleep_ms(300)
    ges=gs.toget()
    if ges=='up' or ges=='down':
        break

    display.draw_string(38,190,'up to start...',color=st7789.color565(50,100,200),\
                        bg=st7789.color565(50,100,200),size=2)
    utime.sleep_ms(300)

oldtime=utime.ticks_ms()
while True:
    start=mymap.gamelogin(display)
    if start==1:
        break
    elif start==-1:
        mymap.gameinstruction(display)
        utime.sleep_ms(500)
    utime.sleep_ms(500)
display.fill(st7789.color565(150,150,250))
display.draw_string(30,105,'Game start in 3s',color=st7789.color565(250,0,0),\
                    bg=st7789.color565(150,150,250),size=2)
utime.sleep_ms(3000)
step=0
count=1
while count<6:
    loc=0
    themap=mymap.mkmap(oldtime,count)
    mymap.draw(display,themap,loc,step)
    while True:
        ges=gs.toget()
        if ges=='up':
            themap=mymap.switch(themap,loc)
            step+=1
            mymap.draw(display,themap,loc,step)
            if mymap.gamepass(themap)==True:
                print('Pass level {}!'.format(count))
                display.fill(st7789.color565(255,255,255))
                display.draw_string(30,85,'Pass level {}!'.format(count),color=st7789.color565(0,0,0),size=2)
                # utime.sleep_ms(2000)
                display.draw_string(30,125,'Later to next lv',color=st7789.color565(0,0,0),size=2)
                utime.sleep_ms(2000)
                count+=1
                break
        elif ges=='right':
            loc=(loc+1)
            if loc>=8:
                step+=8
                loc=loc%8
            mymap.draw(display,themap,loc,step)
        elif ges=='left':
            loc=(loc-1)
            if loc<0:
                step+=8
                loc=(loc+8)%8
            mymap.draw(display,themap,loc,step)

        utime.sleep_ms(500)
print('uuuuuu......')
display.fill(st7789.color565(50,100,200))
display.draw_string(10,40,'Congratulations!',color=st7789.color565(255,0,0),\
                    bg=st7789.color565(0,0,0),size=2)
display.draw_string(10,75,'You pass 5 lvs',color=st7789.color565(255,0,0),\
                    bg=st7789.color565(0,0,0),size=2)
display.draw_string(10,110,'within {} steps!'.format(step),color=st7789.color565(255,0,0),\
                    bg=st7789.color565(0,0,0),size=2)
utime.sleep_ms(3000)

display.fill(st7789.color565(80,50,150))
display.draw_string(20,70,'right to upload score',color=st7789.color565(255,255,255),\
                    bg=st7789.color565(0,0,0),size=2)
display.draw_string(25,105,'down to exit game',color=st7789.color565(255,255,255),\
                    bg=st7789.color565(0,0,0),size=2)
ges=gs.toget()
if ges=='down':
    display.fill(st7789.color565(50,100,200))
    display.draw_string(25,105,'Game exit in 3s',color=st7789.color565(255,255,255),\
                        bg=st7789.color565(50,100,200),size=2)
    utime.sleep_ms(3000)
    exit(0)
elif ges=='right' or ges=='left':
    display.fill(st7789.color565(50,100,200))
    display.draw_string(10,105,'Score upload in 3s',color=st7789.color565(255,255,255),\
                        bg=st7789.color565(0,0,0),size=2)
    utime.sleep_ms(3000)
score=step


def connectWIFI():
    print('Connecting wifi...')
    sta_wlan=network.WLAN(network.STA_IF)
    sta_wlan.active(True)
    sta_wlan.connect('TIKTWINKBOOK','s1234568',security=network.AUTH_PSK)
    while (sta_wlan.config()=='0.0.0.0'):
        utime.sleep(1)
    print('Connected...')


connectWIFI()
url="http://post.blackwalnut.zucc.edu.cn/thingswise/idos/jets/streams/dev/32101184/Gamestation___gamescore.gamescoredata_input_json"

headers={
    "content-type":"application/json",
}
formData={
    'data':{
        "case":"gameStation",
        "sensor":"gameData"
    },
    'auth':{
        'user':'zucc32101184',
        'password':'123abc456d'
    }
}
try:
    formData['data']['step']=score
    res=urequests.post(url,data=ujson.dumps(formData),
                       headers=headers)
    print(res.status_code)
    print(res.text)
except Exception as err:
    print(err)
gc.collect()
utime.sleep_ms(200)
display.fill(st7789.color565(150,200,200))
display.draw_string(30,80,'Data upload sucess!',color=st7789.color565(0,255,0),\
                    bg=st7789.color565(150,200,200),size=2)
display.draw_string(30,110,'right to exit',color=st7789.color565(0,255,0),\
                    bg=st7789.color565(150,200,200),size=2)
utime.sleep_ms(500)
while 1:
    ges=gs.toget()
    if ges=='right' or ges=='left':
        display.fill(st7789.color565(0,0,0))
        break
    utime.sleep_ms(500)